﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IndyMosaic.Models.Syndication;
using System.Web;
using IndyMosaic.Core.Extensions;

namespace IndyMosaic.Models.Podcast
{
    public partial class Entry : Validatable, ISyndicationItem
    {
        
        partial void OnCreated()
        {
            this.Title = string.Empty;
            this.DatePublished = null;
            this.Description = string.Empty;
            this.FileURL = string.Empty;
        }
        partial void OnValidate(System.Data.Linq.ChangeAction action)
        {
            if (!IsValid)
            {
                throw new ApplicationException("Rule violations prevent saving");
            }
        }

        public override IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (string.IsNullOrEmpty(Title))
            {
                yield return new RuleViolation("Title is required", "Title");
            }
            if (string.IsNullOrWhiteSpace(FileURL))
            {
                yield return new RuleViolation("FileURL is required", "FileURL");
            }
        }

        #region ISyndicationItem Members
        public string Content
        {
            get
            {
                return Description;
            }
            set
            {
                Description = value;
            }
        }
        public string MoreTag
        {
            get
            {
                return "<##MORE##>";
            }
        }

        public long EnclosureLength
        {
            get
            {
                return FileLength;
            }
        }

        //public string EnclosureURL
        //{
        //    get { return FileURL; }
        //}
        public string GetEnclosureURL(System.Web.Mvc.UrlHelper urlHelper)
        {
            return urlHelper.GetContentAbsoluteUrl(FileURL);
        }

        #endregion
    }
}
